<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\DataProductApply;
use App\Models\Orm\DataProductDetail;
use App\Models\Orm\Product;
use App\Models\Orm\User;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserInviteCode;
use App\Models\Orm\UserProfile;
use App\Strategies\SexStrategy;

/**
 * 用户工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class ProductFactory extends AbsModelFactory
{


    /**
     * 通过手机号查询用户信息
     */
    public static function getList($type)
    {
       if($type == 1)
       {
           $data = Product::where('status',1)->orderBy('index_type1','desc')->get()->toArray();

       }else{

           $data = Product::where('status',1)->orderBy('index_type2','desc')->get()->toArray();
       }

       foreach ($data as $k =>$v)
       {

           $data[$k]['interest_algorithm'] = Product::getInterestAlgorithmText($v['interest_algorithm']);
           $data[$k]['min_algorithm'] = $v['min_algorithm'].'%';
           $data[$k]['max_algorithm'] = $v['max_algorithm'].'%';
           $data[$k]['fastest'] = $v['fastest'].' '.Product::getFastestTypeText($v['fastest_type']);
           $data[$k]['prepayment'] = Product::getPrepaymentText($v['prepayment']);

       }
        return $data;
    }


    //插入产品流水表
    public static  function addProductApply($user,$info)
    {

        $data['user_id'] = $user->id;
        $data['userphone'] = $user->userphone;
        $data['product_id'] = $info['product_id'];
        $data['terminal'] = $info['terminal'];
        $data['app_name'] = $info['app_name'];
        //获取产品名
        $product = ProductFactory::getProduct($info['product_id']);
        $product_name  = $product ? $product->name : '产品';
        $data['product_name'] = $product_name;
        $data['create_ip'] = Utils::ipAddress();
        $data['create_at'] = date('Y-m-d H:i:s',time());
        $result = DataProductApply::create($data);

        return $result ? true :false;
    }

    //插入产品流水表
    public static  function addProductDetail($user,$info)
    {

        $data['user_id'] = $user->id;
        $data['userphone'] = $user->userphone;
        $data['product_id'] = $info['product_id'];
        $data['terminal'] = $info['terminal'];
        $data['app_name'] = $info['app_name'];
        //获取产品名
        $product = ProductFactory::getProduct($info['product_id']);
        $product_name  = $product ? $product->name : '产品';
        $data['product_name'] = $product_name;
        $data['create_ip'] = Utils::ipAddress();
        $data['create_at'] = date('Y-m-d H:i:s',time());
        $result = DataProductDetail::create($data);

        return $result ? true :false;
    }



    //获取产品信息
    public static  function getProduct($product_id)
    {
        $product = Product::where('id',$product_id)->first();

        return $product ? $product : '';
    }

}